﻿package com.purplemovies.games4nickie.deadly_dots_1.display
{
	import com.purplemovies.events.GameEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;
	import flash.filters.BlurFilter;
	import flash.display.Sprite;
	public class DeadlyDot extends Sprite {
		
		public var color:uint;
		
		private var _ring_radius:int;
		public function set ring_radius(v:int){
			_ring_radius = v;
		}
		public function get ring_radius():int{		return _ring_radius;		}
		
		private var _radius:int;
		public function set radius(v:int){
			_radius = v;
		}
		public function get radius():int{		return _radius;		}
		
		private var _ringer	:Tween;
		private var _dot	:Tween;
		private var _fader	:Tween;
		private var _death	:Tween;
		
		function DeadlyDot( pX:int, pY:int, pRadius:int, pColor:uint, pLoopLenght ){
			
			x = pX; y = pY;
			
			color = pColor;
			
			// Main circle:
			radius = pRadius;
			
			// Ring:
			ring_radius = pRadius+5;
			
			filters = [ new BlurFilter(2,2) ];
			
			var tRandomD = (int(Math.random()*2)) ? 30: -30;
			trace( tRandomD );
			
			new Tween( this, 'x', Regular.easeIn, x, x+tRandomD, pLoopLenght );
			new Tween( this, 'y', Regular.easeIn, y, y+tRandomD, pLoopLenght );
			_ringer = new Tween( this, 'ring_radius', Regular.easeInOut, ring_radius, ring_radius+10, pLoopLenght );
			_fader = new Tween( this, 'alpha', Regular.easeInOut, 0, 1, pLoopLenght );
			
			_death = new Tween( this, 'alpha', Regular.easeInOut, 1, 0, pLoopLenght );
			_death.stop();
			
			_fader.addEventListener( TweenEvent.MOTION_FINISH, onLoopEnd );
			_fader.addEventListener( TweenEvent.MOTION_CHANGE, onLoopChange );
		}
		
		private function onLoopChange(e){
			render();
		}
		
		private function onLoopEnd(e){
			//trace( 'end' );
			_ringer.yoyo();
			_fader.yoyo();
		}
		
		public function die(){
			//trace( 'arse' );
			//_dot.stop();
			_ringer.stop();
			_fader.stop();
			
			_death.addEventListener( TweenEvent.MOTION_FINISH, death );
			_death.start()
		}
		
		private function death(e){
			trace('death');
			dispatchEvent( new GameEvent( GameEvent.DIED, this )  ); // Dispatch died event.
			//parent.removeChild(this);
		}
		
		public function render(){
			graphics.clear();
			
			// Circle:
			graphics.beginFill( color );
			graphics.drawCircle( 0,0, _radius );
			graphics.endFill();
			
			// Ring:
			graphics.lineStyle( 1, color );
			graphics.drawCircle( 0,0, _ring_radius );
		}
		
	}
}